﻿namespace NMortgage.UI.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;

    using Caliburn.Micro;

    using NMortgage.UI.Events;
    using NMortgage.UI.Model;

    public sealed class ExtraPaymentViewModel : PropertyChangedBase
    {
        #region Fields

        private readonly IEventAggregator eventAggregator;

        #endregion Fields

        #region Constructors

        public ExtraPaymentViewModel(IEventAggregator eventAggregator)
            : this(eventAggregator, new ExtraPayment())
        {
        }

        public ExtraPaymentViewModel(IEventAggregator eventAggregator, ExtraPayment item)
        {
            this.eventAggregator = eventAggregator;
            PaymentItem = item;
        }

        #endregion Constructors

        #region Properties

        public Array AvailableFrequencies
        {
            get { return Enum.GetValues(typeof(ExtraPaymentFrequency)); }
        }

        public Array AvailableTypes
        {
            get { return Enum.GetValues(typeof(ExtraPaymentType)); }
        }

        public bool IsRecurring
        {
            get { return PaymentItem.IsRecurring; }
        }

        public ExtraPaymentType SelectedType
        {
            get {
                return PaymentItem.Type;
            }
            set {
                PaymentItem.Type = value;

                NotifyOfPropertyChange(() => SelectedType);
                NotifyOfPropertyChange(() => IsRecurring);
            }
        }
        public bool IsValid
        {
            get
            {
                IValidatable item = this.PaymentItem as IValidatable;
                return (item != null) ? PaymentItem.IsValid : true;
            }
        }

        public ExtraPayment PaymentItem
        {
            get;
            set;
        }

        #endregion Properties

        #region Methods

        public void Cancel()
        {
            eventAggregator.Publish(new CancelEvent(this));
        }

        public void Finish()
        {
            if (IsValid)
            {
                eventAggregator.Publish(new ProfileItemAdded(PaymentItem));
            }
        }

        #endregion Methods
    }
}